using System;

namespace ZQNB.Common.Encrypt
{
    public enum PasswordFormat
    {
        Clear = 0,
        Hashed = 1,
        Encrypted = 2,
    }

    public class PasswordEncryptor
    {
        private readonly NbEncryptHelper _helper;

        public PasswordEncryptor(NbEncryptHelper helper)
        {
            _helper = helper;
        }

        public string Encrypt(string password, PasswordFormat format = PasswordFormat.Hashed, string salt = "")
        {
            string encryptPass;
            switch (format)
            {
                case PasswordFormat.Clear:
                    encryptPass = password;
                    break;
                case PasswordFormat.Hashed:
                    encryptPass = _helper.Hash(password + salt);
                    break;
                case PasswordFormat.Encrypted:
                    encryptPass = _helper.EncryptSymmetric(password);
                    break;
                default:
                    throw new ArgumentOutOfRangeException("format");
            }
            return encryptPass;
        }

        public string GenerateRandomSalt()
        {
            return _helper.GenerateRandomString(4, 97, 122);
        }
    }
}